import axios from 'axios';
import {Modal,Notice} from 'iview';
import Cookies from 'js-cookie';


let util = {};


util.devPost = function(url, params, callback,notice){
  let axiosIns = axios.create({
    timeout: 30000,
    headers: {
      "Content-Type": "application/json",
      "Authorization": Cookies.get('angel-bo-token')
    }
  });

  axiosIns.post(url, params).then(res => {
    //console.log(res);
    const {status, msg} = res.data;
    if (status === '200') {
      if(notice){
        Notice.success({
          title: '成功',
          desc: '操作成功'
        });
      }
      callback(res.data.data);
    } else if (status !== '200') {
      Modal.info({
        title: status,
        content: msg
      });

    }
  }).catch(err => {

    if (err.response != undefined && err.response.status == 504) {
      Modal.error({
        title: '网络错误',
        content: '网络不通'
      });
    } else {
      Modal.error({
        title: '错误',
        content: '系统异常'
      });
      console.error(err);
    }

  });
};


util.post = function (url, params, callback,notice) {
  let axiosIns = axios.create({
    baseURL: '/api',
    timeout: 30000,
    headers: {
      "Content-Type": "application/json",
      "Authorization": Cookies.get('angel-bo-token')
    }
  });

  axiosIns.post(url, params).then(res => {
    //console.log(res);
    const {status, msg} = res.data;
    if (status === '200') {
      if(notice){
        Notice.success({
          title: '成功',
          desc: '操作成功'
        });
      }
      callback(res.data.data);
    } else if (status !== '200') {
      Modal.info({
        title: status,
        content: msg
      });

    }
  }).catch(err => {

    if (err.response != undefined && err.response.status == 504) {
      Modal.error({
        title: '网络错误',
        content: '网络不通'
      });
    } else {
      Modal.error({
        title: '错误',
        content: '系统异常'
      });
      console.error(err);
    }

  });

};

util.queryPost = function (url, params, callback,notice) {
  let axiosIns = axios.create({
    baseURL: '/api',
    timeout: 30000,
    headers: {
      "Content-Type": "application/json",
      "Authorization": Cookies.get('angel-bo-token')
    }
  });

  axiosIns.post(url, null, {
    params: params
  }).then(res => {
    //console.log(res);
    const {status, msg} = res.data;
    if (status === '200') {
      if(notice){
        Notice.success({
          title: '成功',
          desc: '操作成功'
        });
      }

      callback(res.data.data);
    } else if (status !== '200') {
      Modal.info({
        title: status,
        content: msg
      });

    }
  }).catch(err => {
    if (err.response != undefined && err.response.status == 504) {
      Modal.error({
        title: '网络错误',
        content: '网络不通'
      });
    } else {
      Modal.error({
        title: '错误',
        content: '系统异常'
      });
      console.error(err);
    }
  });

};


//tree 转 list
util.treeToList = function (tree,list) {
  tree.forEach(item => {
    if(item.children){
      util.treeToList(item.children,list);
    }
    item.children = null;
    if(item.checked){
      list.push(item);
    }
  });
};


util.permissionTreeToList = function (tree) {
  for(let i = 0 ; i < tree.length ; i++){
    //把tree上的button都移走
    console.log(tree[i]);
    if(tree[i].children){
      util.permissionTreeToList(tree[i].children);
    }
    if(!tree[i].checked){
      tree.splice(i);
    }else if(tree[i].menuType === 2){
      tree.splice(i);
    }
  }
};


util.formatDate =  function(date, fmt) {
  let o = {
    'M+': date.getMonth() + 1, // 月份
    'd+': date.getDate(), // 日
    'h+': date.getHours(), // 小时
    'm+': date.getMinutes(), // 分
    's+': date.getSeconds(), // 秒
    'S': date.getMilliseconds() // 毫秒
  }
  if (/(y+)/.test(fmt)) {
    fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length))
  }
  for (var k in o) {
    if (new RegExp('(' + k + ')').test(fmt)) {
      fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? (o[k]) : (('00' + o[k]).substr(('' + o[k]).length)))
    }
  }
  return fmt
};





export default util;
